package com.groupwork.resourcemonitoruserserver.controller;


import com.groupwork.resourcemonitorcommon.common.BaseResponse;
import com.groupwork.resourcemonitorcommon.common.ErrorCode;
import com.groupwork.resourcemonitorcommon.common.ResultUtils;
import com.groupwork.resourcemonitorcommon.exception.BusinessException;
import com.groupwork.resourcemonitormodel.model.user.UserLoginDTO;
import com.groupwork.resourcemonitormodel.model.user.UserRegisterDTO;
import com.groupwork.resourcemonitoruserserver.service.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 用户控制器
 *
 * @author 张杰名
 * @time 2023/10/30
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api("用户模块")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     * @param userRegisterDTO
     * @return
     */
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterDTO userRegisterDTO){
        //判断前端传来的实体对象是否为空
        if (userRegisterDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return userService.userRegister(userRegisterDTO);
    }

    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    public BaseResponse<String> userLogin(@RequestBody UserLoginDTO userLoginDTO){
        //判断前端传来的实体对象是否为空
        if (userLoginDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return userService.userLogin(userLoginDTO);
    }

    /**
     * 测试方法， @RequiresPermissions("发布任务者")是这个方法的权限
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/main")
    @RequiresPermissions("发布任务")
    public String test(@RequestBody UserLoginDTO userLoginDTO){
        return "有权限";
    }

}
